package com.project.web.admin.config.freemarker;

import com.project.web.admin.utils.freemarker.directive.StdCodeDirective;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;

/**
 * Freemarker配置类
 * User: WYY
 * Date: 2019-04-11
 */
@Configuration
public class FreeMarkerConfig {

    @Autowired
    protected freemarker.template.Configuration configuration;

    @Autowired
    protected StdCodeDirective stdCodeDirective;

    @Autowired
    private ServletContext servletContext;

    @PostConstruct
    public void setSharedVariable() throws TemplateModelException {
        // ftl自定义方法
        configuration.setSharedVariable("stdCode", stdCodeDirective);

        // ftl常量
        configuration.setSharedVariable("base", servletContext.getContextPath());
    }

}